package com.qianfeng.teachdemo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.qianfeng.teachdemo.R;
import com.qianfeng.teachdemo.adapter.LeftAdapter;
import com.qianfeng.teachdemo.bean.MenuIconMenu;
import com.qianfeng.teachdemo.ui.LoginActivity;
import com.qianfeng.teachdemo.ui.eventBus.EventBusActivity;
import com.qianfeng.teachdemo.ui.webservice.WebServiceActivity;

import java.util.ArrayList;

/**
 * 侧滑菜单fragment
 */
public class LeftFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private SlidingMenu menu;

    private OnFragmentInteractionListener mListener;

    public static LeftFragment newInstance(String param1, String param2) {
        LeftFragment fragment = new LeftFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public LeftFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_left_layout, container, false);
        ArrayList<String> list = new ArrayList<String>();
        String[] contents = getResources().getStringArray(R.array.menu_left_array);
        for (int i = 0; i < contents.length; i++) {
            list.add(contents[i]);
        }
        TextView menuTitle = (TextView) view.findViewById(R.id.menu_title);
        menuTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, getAactionBarHeight()));
        ListView listView = (ListView) view.findViewById(R.id.menu_left_list);
        listView.setOnItemClickListener(new leftMenuOnitemClick(list));
        listView.setAdapter(new LeftAdapter(activity, MenuIconMenu.getMenuList()));
        return view;
    }


    /**
     * 左边侧滑菜单点击事件
     */
    private class leftMenuOnitemClick implements AdapterView.OnItemClickListener {
        private ArrayList<String> list;

        public leftMenuOnitemClick(ArrayList<String> list) {
            super();
            this.list = list;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0:
                    Intent intent1 = new Intent(activity, LoginActivity.class);
                    startActivity(intent1);
                    break;
                case 1:
                    Intent intent = new Intent(activity, LoginActivity.class);
                    startActivity(intent);
                    break;
                case 2:
                    getChildFragmentManager().beginTransaction().replace(R.id.container, new ValidationFragment()).commit();
                    mListener.onFragmentInteraction();
                    break;
                case 3:
                    startActivity(new Intent(activity, EventBusActivity.class));
                    break;
                case 4:
//                    upload();
                    break;
                case 5:
                    startActivity(new Intent(activity, WebServiceActivity.class));
                    break;
            }
        }
    }

    private int getAactionBarHeight() {
        int actionBarHeight = 0;
        TypedValue tv = new TypedValue();
        if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
        }
        return actionBarHeight;
    }



    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction();
    }

    public OnFragmentInteractionListener getmListener() {
        return mListener;
    }

    public void setmListener(OnFragmentInteractionListener mListener) {
        this.mListener = mListener;
    }
}
